module(..., package.seeall)

require "nvm"
require "mqttOutMsg"
require "getLbsLoc"
require "modbusrtu"
require "regularlyUploadData"
require "pressureRealTime"
require "configurationParam"
-- 初始化nvm获取配置参数
nvm.init("config.lua", true)

-- 系统参数初始化
local pKey, sn = nvm.get("PKey"), nvm.get("Sn")

-- 其他参数初始化
local errInfo, sysTime = "", ""

-- 系统端数据召读
local function dataCall(jsondata)
    local ts, seq
    if jsondata["seq"] then seq = jsondata["seq"] end
    ts = os.time() .. "000" -- 系统时间
    local devInfoCallTopic, devInfo = terminalSendTheme.CallingDeviceData(ts,
                                                                          seq)

    mqttOutMsg.insertMsgUser(devInfoCallTopic, devInfo, 0)
end
--- MQTT客户端数据接收处理
-- @param mqttClient，MQTT客户端对象
-- @return 处理成功返回true，处理出错返回false
-- @usage mqttInMsg.proc(mqttClient)
function proc(mqttClient)
    local result, data, receiveMsg, receiveMsgHex
    while true do
        result, data = mqttClient:receive(60000, "APP_SOCKET_SEND_DATA")
        -- 接收到数据
        if result then
            receiveMsg = data.payload
            local tjsondata, resultMqtt, errinfo = json.decode(receiveMsg)
            log.info("resultMqtt", resultMqtt, "errinfo", errinfo)
            log.info("mqtt payload", receiveMsg, "data.topic", data.topic)
            if data.topic == "/cloud/single/" .. pKey .. "/" .. sn .. "/set" then -- 参数设置
                log.info("设置参数start！")
                if resultMqtt and type(tjsondata) == "table" then
                    log.info("设置参数table！")
                    while pressureRealTime.LoopReadOrNot do
                        sys.wait(100)
                    end
                    mqttInMsgKeson.ParamSetOrNot = true
                    configurationParam.ParamSet(tjsondata,
                                                      mqttInMsgKeson.ParamSetOrNot,
                                                      "XinAo")
                    mqttInMsgKeson.ParamSetOrNot = false
                else
                    log.info("设置参数不是table！")
                    sysTime = string.format("%04d-%02d-%02d %02d:%02d:%02d",
                                            os.date("*t").year,
                                            os.date("*t").month,
                                            os.date("*t").day,
                                            os.date("*t").hour,
                                            os.date("*t").min, os.date("*t").sec)
                    errInfo = errInfo .. "[" .. sysTime .. "]" ..
                                  "[json.decode error:]" .. errinfo .. "\n"
                    print("json.decode error", errinfo)
                end
            elseif data.topic == "/cloud/single/" .. pKey .. "/" .. sn .. "/rtg" then -- 设备上传实时数据反馈

            elseif data.topic == "/cloud/single/" .. pKey .. "/" .. sn ..
                "/info" then -- 启动数据上报返回

            elseif data.topic == "cloud/single/" .. pKey .. "/" .. sn .. "/call" then -- 系统端召读数据
                log.info("数据召读开始！")
                if resultMqtt and type(tjsondata) == "table" then
                    dataCall(tjsondata)
                else
                    log.info("设置参数不是table！")
                    sysTime = string.format("%04d-%02d-%02d %02d:%02d:%02d",
                                            os.date("*t").year,
                                            os.date("*t").month,
                                            os.date("*t").day,
                                            os.date("*t").hour,
                                            os.date("*t").min, os.date("*t").sec)
                    errInfo = errInfo .. "[" .. sysTime .. "]" ..
                                  "[json.decode error:]" .. errinfo .. "\n"
                    print("json.decode error", errinfo)
                end
            elseif data.topic == "/cloud/single/" .. pKey .. "/" .. sn ..
                "/change" then -- 现场修改参数上传系统端返回

            elseif data.topic == "ONLINE/SET" then
            end
        else
            break
        end
    end
    return result or data == "timeout" or data == "APP_SOCKET_SEND_DATA"
end
